package com.sihan.swvp.action.jc.bzzy;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.entity.jc.bzzy.BzzyMajor;
import com.sihan.swvp.service.jc.bzzy.BzzyMajorService;
import com.sihan.swvp.service.sys.LogService;

@Controller
@RequestMapping("/bzzy/BzzyMajorAction")
public class BzzyMajorAction extends BaseAction{

	@Autowired
	private BzzyMajorService bzzyMajorService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping("/getBzzyMajor")
	public Object getBzzyMajor(@Param("bzzyCode")String bzzyCode,@Param("bzzyName")String bzzyName,@Param("page")Integer page,@Param("limit")Integer limit){
		try {
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("bzzyCode", bzzyCode);
			params.put("bzzyName", bzzyName);
			return pageData(bzzyMajorService.getBzzyMajor(params,page,limit));
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/addBzzyMajor")
	public Object addBzzyMajor(BzzyMajor bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			Map<String, Object> validate=bzzyMajorService.getBzzyMajorValidate(bean);
			int nameNubmer=Integer.parseInt(validate.get("nameNubmer").toString());
			int codeNumber=Integer.parseInt(validate.get("codeNubmer").toString());
			if(nameNubmer !=0){
				return failMsg("该专业名称已存在");
			}else if(codeNumber !=0){
				return failMsg("该专业编码已存在");
			}
			bzzyMajorService.addBzzyMajor(bean);
			logService.addUserLog("添加专业", "专业", getLoginUser().getId(), "添加专业："+bean.getBzzyName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("添加专业", "专业", getLoginUser().getId(), "添加专业："+bean.getBzzyName(),getRemoteAddr(),0,0);
			return failMsg("添加失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/updateBzzyMajor")
	public Object updateBzzyMajor(BzzyMajor bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			Map<String, Object> validate=bzzyMajorService.getBzzyMajorValidate(bean);
			int nameNubmer=Integer.parseInt(validate.get("nameNubmer").toString());
			int codeNumber=Integer.parseInt(validate.get("codeNubmer").toString());
			if(nameNubmer !=0){
				return failMsg("专业名称已存在");
			}else if(codeNumber !=0){
				return failMsg("专业编码已存在");
			}
			bzzyMajorService.updateBzzyMajor(bean);
			logService.addUserLog("修改专业", "专业", getLoginUser().getId(), "修改专业："+bean.getBzzyName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("修改专业", "专业", getLoginUser().getId(), "修改专业："+bean.getBzzyName(),getRemoteAddr(),0,0);
			return failMsg("修改失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteBzzyMajor")
	public Object deleteBzzyMajor(@Param("id")Integer id,String bzzyName){
		try {
			bzzyMajorService.deleteBzzyMajor(id);
			logService.addUserLog("删除专业", "专业", getLoginUser().getId(), "删除专业："+bzzyName,getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除专业", "专业", getLoginUser().getId(), "删除专业："+bzzyName,getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
}
